package com.merelyb.controller;

import com.merelyb.business.service.affpay.BusinessAffpayInfoService;
import com.merelyb.data.service.service.common.Const;
import com.merelyb.data.service.service.mybatis.bean.affpay.AffpayCommission;
import com.merelyb.data.service.service.mybatis.bean.affpay.AffpayInfo;
import com.merelyb.data.service.service.mybatis.bean.affpay.AffpayPaymentFrequency;
import com.merelyb.data.service.service.mybatis.bean.affpay.AffpayPaymethod;
import com.merelyb.util.JsonUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by LiM on 2017/12/9.
 */
@Controller
public class IndexController {

    @Autowired
    BusinessAffpayInfoService businessAffpayInfoService;
    private int iPage = 10;

    /**
     * 无条件的显示
     *
     * @param affpayInfo
     * @return
     */
    private List<AffpayInfo> getAffpayInfo(AffpayInfo affpayInfo) {
        return businessAffpayInfoService.getAffpayInfo(affpayInfo);
    }

    /**
     * 获取所有支付类型
     *
     * @param
     * @return
     */
    private List<String> getAffpayMethodType() {
        AffpayPaymethod affpayPaymethod = new AffpayPaymethod();
        affpayPaymethod.setIsDelete(0);
        return businessAffpayInfoService.getPayMethodType(affpayPaymethod);
    }

    /**
     * 获取所有支付周期
     *
     * @return
     */
    private List<String> getAffpayFreType() {
        AffpayPaymentFrequency affpayPaymentFrequency = new AffpayPaymentFrequency();
        affpayPaymentFrequency.setIsDelete(0);
        return businessAffpayInfoService.getPayFreType(affpayPaymentFrequency);
    }

    /**
     * 获取满足支付方式的编号
     *
     * @param sAffpayMethod
     * @return
     */
    private List<Long> getAffpayMethodByTerm(String sAffpayMethod) {
        if (sAffpayMethod.equals("") || sAffpayMethod.equals("Pay Method") || sAffpayMethod == null) return null;
        AffpayPaymethod affpayPaymethod = new AffpayPaymethod();
        affpayPaymethod.setPaymethod(sAffpayMethod);
        affpayPaymethod.setIsDelete(0);
        List<Long> idList = businessAffpayInfoService.getPayMethodAffpayIds(affpayPaymethod);
        if (idList == null) idList = new ArrayList<>();
        return idList;
    }

    /**
     * 获取满足周期的编号
     *
     * @param sPayFre
     * @return
     */
    private List<Long> getAffpayFreByTerm(String sPayFre) {
        if (sPayFre.equals("") || sPayFre.equals("Pay Frequency") || sPayFre == null) return null;
        AffpayPaymentFrequency affpayPaymentFrequency = new AffpayPaymentFrequency();
        affpayPaymentFrequency.setIsDelete(0);
        affpayPaymentFrequency.setPaymentFrequency(sPayFre);
        List<Long> idList = businessAffpayInfoService.getPayFreAffpayIds(affpayPaymentFrequency);
        if (idList == null) idList = new ArrayList<>();
        return idList;
    }

    /**
     * 获取满足任务类型的编号
     *
     * @param sPayComm
     * @return
     */
    private List<Long> getAffpayCommissionByTerm(String sPayComm) {
        if (sPayComm.equals("") || sPayComm.equals("Pay Commission") || sPayComm == null) return null;
        AffpayCommission affpayCommission = new AffpayCommission();
        affpayCommission.setIsDelete(0);
        affpayCommission.setCommissionType(sPayComm);
        List<Long> idList = businessAffpayInfoService.getPayCommissionAffpayIds(affpayCommission);
        if (idList == null) idList = new ArrayList<>();
        return idList;
    }

    /**
     * 获取满足最小支付的编号
     *
     * @param sPayMin
     * @return
     */
    private List<Long> getAffpayMinByTerm(String sPayMin) {
        if (sPayMin.equals("") || sPayMin.equals("Min-Pay") || sPayMin == null) return null;
        AffpayInfo affpayInfo = new AffpayInfo();
        affpayInfo.setIsDelete(0);
        affpayInfo.setMinimumPayment(sPayMin);
        List<Long> idList = businessAffpayInfoService.getPayMinAffpayIds(affpayInfo);
        if (idList == null) idList = new ArrayList<>();
        return idList;
    }

    /**
     * 获取任务类型
     *
     * @return
     */
    public List<String> getAffpayCommission() {
        AffpayCommission affpayCommission = new AffpayCommission();
        affpayCommission.setIsDelete(0);
        return businessAffpayInfoService.getPayCommissionType(affpayCommission);
    }

    /**
     * 获取所有最小支付类型
     *
     * @return
     */
    public List<String> getAffpayMinType() {
        AffpayInfo affpayInfo = new AffpayInfo();
        affpayInfo.setIsDelete(0);
        return businessAffpayInfoService.getPayMinType(affpayInfo);
    }

    /**
     * 获取相同编号
     *
     * @param listOne
     * @param listTwo
     * @return
     */
    private List<Long> checkHad(List<Long> listOne, List<Long> listTwo) {
        if (listOne == null) return listTwo;
        if (listTwo == null) return listOne;
        Collection<Long> colOne = new ArrayList<>(listOne);
        Collection<Long> colTwo = new ArrayList<>(listTwo);
        Collection<Long> colHad = new ArrayList<>();
        colHad.addAll(colOne);
        colHad.addAll(colTwo);
        colOne.removeAll(colTwo);
        colTwo.removeAll(colOne);
        colHad.removeAll(colOne);
        colHad.removeAll(colTwo);
        List listReturn = new ArrayList(colHad);
        return listReturn;

    }

    /***
     * 获取编号
     * @param sPayMenthd
     * @param sPayFre
     * @param sPayMin
     * @param sPayComm
     * @return
     */
    private List<Long> getNeedIds(String sPayMenthd, String sPayFre, String sPayMin, String sPayComm) {
        List<Long> affpayMethodList = getAffpayMethodByTerm(sPayMenthd);
        List<Long> affpayFreList = getAffpayFreByTerm(sPayFre);
        List<Long> affpayMinList = getAffpayMinByTerm(sPayMin);
        List<Long> affpayCommList = getAffpayCommissionByTerm(sPayComm);

        List<Long> listResult = checkHad(affpayMethodList, affpayFreList);
        listResult = checkHad(listResult, affpayMinList);
        listResult = checkHad(listResult, affpayCommList);
        return listResult;
    }

    /**
     * 获取个数
     *
     * @param
     * @return
     */
    private int getAffpayInfoNum(String sPayMenthd, String sPayFre, String sPayMin, String sPayComm) {
        List<Long> affpayInfoIds = getNeedIds(sPayMenthd, sPayFre, sPayMin, sPayComm);
        if (affpayInfoIds == null) {
            AffpayInfo affpayInfo = new AffpayInfo();
            affpayInfo.setIsDelete(0);
            return businessAffpayInfoService.getAffpayNum(affpayInfo);
        } else {
            return affpayInfoIds.size();
        }
    }

    /**
     * 按条件获取联盟数据
     *
     * @param sPayMenthd
     * @param sPayFre
     * @param sPayMin
     * @param sPayComm
     * @param iPageNum
     * @return
     */
    private List<AffpayInfo> getNeedAffpayInfo(String sPayMenthd, String sPayFre, String sPayMin, String sPayComm, int iPageNum) {
        List<AffpayInfo> affpayInfoList = null;
        List<Long> affpayInfoIds = getNeedIds(sPayMenthd, sPayFre, sPayMin, sPayComm);
        if (affpayInfoIds == null) {
            AffpayInfo affpayInfo = new AffpayInfo();
            affpayInfo.setIsDelete(0);
            affpayInfo.setOffset((iPageNum - 1) * iPage);
            affpayInfo.setMax(iPage);
            affpayInfoList = getAffpayInfo(affpayInfo);
        } else {
            List<Long> IdLists = new ArrayList<>();
            int iNum = 0;
            if (affpayInfoIds.size() < (iPageNum - 1) * iPage) {
                return null;
            } else if (affpayInfoIds.size() > iPageNum * iPage) {
                iNum = iPageNum * iPage;
            } else {
                iNum = affpayInfoIds.size();
            }
            for (int i = (iPageNum - 1) * iPage; i < iNum; i++) {
                if (i < 0) continue;
                IdLists.add(affpayInfoIds.get(i));
            }
            affpayInfoList = businessAffpayInfoService.getAffpayInfoByIds(IdLists);
        }
        return affpayInfoList;
    }

    /**
     * 起始页
     *
     * @return
     */
    @RequestMapping("index.do")
    public String getIndex(HttpServletRequest request) {
        List<String> affPayMethodList = getAffpayMethodType();
        request.setAttribute("payMethodList", affPayMethodList);

        List<String> affpayFreList = getAffpayFreType();
        request.setAttribute("payFreList", affpayFreList);

        List<String> affpayMinList = getAffpayMinType();
        request.setAttribute("payMinList", affpayMinList);

        List<String> affpayCommList = getAffpayCommission();
        request.setAttribute("payCommList", affpayCommList);

        List<AffpayInfo> affpayInfoList = getNeedAffpayInfo("", "", "", "", 1);
        int iTotalNum = getAffpayInfoNum("", "", "", "");
        int iPageNum = iTotalNum / iPage + (iTotalNum % iPage == 0 ? 0 : 1);
        request.setAttribute("lists", affpayInfoList);
        request.setAttribute("nTotal", iTotalNum);
        request.setAttribute("bNum", 0 * iPage);
        request.setAttribute("eNum", 1 * iPage > iTotalNum ? iTotalNum : 1 * iPage);
        request.setAttribute("pTotal", iPageNum);
        request.setAttribute("cPage", 1);
        return "JSP/index";
    }

    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping(method = RequestMethod.POST, value = "page/info.do")
    public void getCurPage(HttpServletRequest request, HttpServletResponse response) {
        String sPayMethod = request.getParameter("PayMethod") != null ? request.getParameter("PayMethod") : "";
        String sPayFre = request.getParameter("PayFre") != null ? request.getParameter("PayFre") : "";
        String sMinPay = request.getParameter("MinPay") != null ? request.getParameter("MinPay") : "";
        String sPayCom = request.getParameter("PayCom") != null ? request.getParameter("PayCom") : "";
        int iQueryPage = request.getParameter("queryPage") != null && NumberUtils.isNumber(request.getParameter("queryPage")) ?
                Integer.parseInt(request.getParameter("queryPage")) : 1;
        List<AffpayInfo> affpayInfoList = getNeedAffpayInfo(sPayMethod, sPayFre, sMinPay, sPayCom, iQueryPage);
        int iTotalNum = getAffpayInfoNum(sPayMethod, sPayFre, sMinPay, sPayCom);
        int iPageNum = iTotalNum / iPage + (iTotalNum % iPage == 0 ? 0 : 1);
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put(Const.S_IS_SUCCESSED, true);
        jsonMap.put("lists", affpayInfoList);
        jsonMap.put("nTotal", iTotalNum);
        jsonMap.put("pTotal", iPageNum);
        jsonMap.put("bNum", (iQueryPage - 1) * iPage + 1);
        jsonMap.put("eNum", iQueryPage * iPage > iTotalNum ? iTotalNum : iPage * iQueryPage);
        jsonMap.put("cPage", iQueryPage);
        jsonMap.put(Const.S_MSG, "成功！");
        try {
            response.getWriter().write(JsonUtils.obj2Json(jsonMap));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
